<?php session_start(); 
if ( $_SESSION["level"] == null ) header("location:login.php");
?>
<!DOCTYPE html>
<html>
 <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <link rel="shortcut icon" href="./files/img/favicon.ico">
  <title>IAD - Euromedica</title>

<!--   <link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/redmond/jquery-ui.css" rel="Stylesheet" /> -->
  <link type="text/css" href="./files/jquery-ui/css/smoothness/jquery-ui-1.8.21.custom.css" rel="Stylesheet" />	
  <script type="text/javascript" src="./files/jquery-1.6.4.js"></script>
  <script type="text/javascript" src="./files/jquery-ui/js/jquery-ui-1.8.16.custom.min.js"></script>
  <script type="text/javascript" src="./files/padolsey-jQuery-Plugins-01abd79/sortElements/jquery.sortElements.js"></script>

  <style type="text/css">
    @import "style2.css";
  </style>

  <script type="text/javascript" src="./scripts.js"></script>
 </head>



 <?php 
   include 'sql.php';
   $status = $_GET['req'];
   $filter = $_GET['filter'];
 ?>

<body>

<div id="access">
<?php showRightPanel(); ?>
</div> <!--End of access-->

  <!--
  Start Header Area
  -->
<div id="header">
</div><!-- End of HEADER -->

<div id="ribbon">
  <table>
    <tr>
    </tr>
  </table>
</div><!--End of ribbon-->

 <!--
  Start Left Panel Area
  -->
 <?php showLeftPanel($_SESSION['level'], $_SESSION['user']); ?>
    <!--
    End of Left_panel
    -->

 <!--
  Start Container Area
  -->
 <div id="container">
  <div id="content"><!-- Content Area-->
     <div id="requests_table">
	<?php showFilters($status); ?>
	<table id="team" class="requests_table">
	  <?php
	      if ($status == "approved" || $status == "rejected" || $status == "ready" || $status == "misc" || $status == "pending") { 
		$pipa = strtoupper($status);
		print("<h2>$pipa</h2>");
		$count = showRequests($status, $filter);
	      } //end of if
	      else {
		print("<h1>I said NO</h1>");
	      }
	    ?>
	      <?php if ($_SESSION['level'] == "admin") { ?>
	      <input type="button" id="check" name="Check" value="Delete All" style="margin: 0 auto;"<?php if( $count == 0){?> disabled="disabled"<?php }//end of if ?> >
	    <div id="dialog-delete" class="<?php echo $status?>" hidden>
		<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
	    </div>
	    <input type="button" class="add_btn" name="add" value="Add" style="margin: 0 auto;">
	  <?php } ?>
	  <br><br>
	</table>
	<div id="dialog-add" hidden>
	  <form id="add_con">
	  <fieldset>
	    <label for="categ">Κατηγορία:</label><br>
	      <select id="category" name="category">
		<option value="approved" label="">Approved</option>
		<option value="ready" label="">Ready</option>
		<option value="pending" label="">Pending</option>
		<option value="rejected" label="">Rejected</option>
		<option value="misc" label="">Misc</option>
	      </select><br>
	    <label for="req_name">Ονοματεπώνυμο:</label><br>
	    <input type="text" name="req_name" id="req_name" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="pass_num">Αρ. Διαβατηρίου:</label><br>
	    <input type="text" name="pass_number" id="pass_number" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="clinic">Μονάδα:</label><br>
	    <input type="text" name="clinic" id="clinic" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="addm_num">Αρ. Εισαγωγής:</label><br>
	    <input type="text" name="addm_num" id="addm_number" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="pat_code">Κωδ. Ασθενούς:</label><br>
	    <input type="text" name="pat_code" id="patient_code" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="doctor">Υπεύθυνος Ιατρός:</label><br>
	    <input type="text" name="doctor" id="doctor" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="request">Αίτημα:</label><br>
	    <input type="text" name="request" id="req" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="est_cost">Εκτ. Κόστος:</label><br>
	    <input type="text" name="est_cost" id="estimated_cost" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="date_received">Ημ/νία Παραλαβής:</label><br>
	    <input type="text" name="date_received" id="date_rec" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="date_approved">Ημ/νία Έγκρισης:</label><br>
	    <input type="text" name="date_approved" id="date_app" class="text ui-widget-content ui-corner-all" /><br>
	    <label for="notes">Παρατηρήσεις:</label><br>
	    <input type="text" name="notes" id="observations" class="text ui-widget-content ui-corner-all" /><br>
	    <br>
	  </fieldset>
	</form>
	</div>
    </div><!--END of requests_table-->
  </div><!--END of content-->
</div><!--END of container-->

<?php showFooter(); ?><!--END of footer-->

<script>
$(document).ready(function() {
   var flag = false;
   $(".edit_tr").hover(
     function(){
      var id = $(this).attr('id');
      $("#del-btn-"+id).show();
      $("#del-btn-"+id).click(
	function(){
	  flag = true;
	  $("#"+id).remove();
	var dataString = 'reqi_id='+id;

	  $.ajax({
	    type: "POST",
	    url: "sql.php",
	    data: dataString,
	    cache: false,
	    success: function(html) {
	    }
	  });
	});//end of click for #delete
    },
    function(){
     var id = $(this).attr('id');
      flag = false;
      $("#delete-"+id).hide();
   });//end of hover

$(".edit_tr").hover(function() {
//   if (flag == false) {
    var ID=$(this).attr('id');
    $("#edit-btn-"+ID).click( function() {
      $("#name-"+ID).hide();
      $("#pass_num-"+ID).hide();
      $("#med_unit-"+ID).hide();
      $("#addm_num-"+ID).hide();
      $("#pat_code-"+ID).hide();
      $("#doc_name-"+ID).hide();
      $("#request-"+ID).hide();
      $("#est_cost-"+ID).hide();
      $("#date_received-"+ID).hide();
      $("#date_approved-"+ID).hide();
      $("#notes-"+ID).hide();
      $("#status-"+ID).hide();
      $("#name-input-"+ID).show();
      $("#pass_num-input-"+ID).show();
      $("#med_unit-input-"+ID).show();
      $("#addm_num-input-"+ID).show();
      $("#pat_code-input-"+ID).show();
      $("#doc_name-input-"+ID).show();
      $("#request-input-"+ID).show();
      $("#est_cost-input-"+ID).show();
      $("#date_received-input-"+ID).show();
      $("#date_approved-input-"+ID).show();
      $("#notes-input-"+ID).show();
      $("#status-input-"+ID).show();
    });
//------------------------------------------------
// }//end of if
//--------------------------------------------
    }).change(function() {
      var ID=$(this).attr('id');
      var name=$("#name-input-"+ID).val();
      var pass_num=$("#pass_num-input-"+ID).val();
      var med_unit=$("#med_unit-input-"+ID).val();
      var addm_num=$("#addm_num-input-"+ID).val();
      var pat_code=$("#pat_code-input-"+ID).val();
      var doc_name=$("#doc_name-input-"+ID).val();
      var request=$("#request-input-"+ID).val();
      var est_cost=$("#est_cost-input-"+ID).val();
      var date_received=$("#date_received-input-"+ID).val();
      var date_approved=$("#date_approved-input-"+ID).val();
      var notes=$("#notes-input-"+ID).val();
      var status=$("#status-input-"+ID).val();

      var dataString = 'req_id='+ ID + '&name=' + name + '&pass_num='+pass_num+'&med_unit='+med_unit+
		      '&addm_num='+addm_num+'&pat_code='+pat_code+'&doc_name='+doc_name+
		      '&request='+request+'&est_cost='+est_cost+'&date_received='+date_received+
		      '&date_approved='+date_approved+'&notes='+notes+'&status_input='+status;
      $("#score-"+ID).html('<img src="load.gif" />'); // Loading image
   
	$.ajax({
	  type: "POST",
	  url: "sql.php",
	  data: dataString,
	  cache: false,
	  success: function(html) {
	    $("#name-"+ID).html(name);
	    $("#pass_num-"+ID).html(pass_num);
	    $("#med_unit-"+ID).html(med_unit);
	    $("#addm_num-"+ID).html(addm_num);
	    $("#pat_code-"+ID).html(pat_code);
	    $("#doc_name-"+ID).html(doc_name);
	    $("#request-"+ID).html(request);
	    $("#est_cost-"+ID).html(est_cost);
	    $("#date_received-"+ID).html(date_received);
	    $("#date_approved-"+ID).html(date_approved);
	    $("#notes-"+ID).html(notes);
	    $("#status-"+ID).html(status);
	  }
	});
      }); //end of change
  // Edit input box click action
  $(".editbox").mouseup(function() {
    return false;
  }); // end of mouseup
  // Outside click action
  $(document).mouseup(function() {
    $(".editbox").hide();
    $(".text").show();
  }); // end of mouseup
  $(document).hover(function(){
    var ID=$(this).attr('id');
    $("#edit-"+ID).hide();
    $("#delete-"+ID).hide();
  });//end of hover
  $(".editbox").keydown(function(event) {
    if (event.which == 13){
      //$(".editbox").hide();
      //$("#edit-"+id).hide();
      //$("#delete-"+id).hide();
      //$(".text").show();
    }//end of if
  }); //end of keydown
$("#check").click(function(){
  var req_type = $("#dialog-delete").attr('class');
  var f = false;
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
      $( "#dialog-delete" ).dialog({
	title: "Delete all requests?",
	resizable: false,
	height:200,
	modal: true,
	buttons: {
	  "Delete all items": function() {
	    f = true;
	    $(this).dialog("close");
	    $(".edit_tr").each(function(index){
	      var id = $(this).attr('id');
	      $(this).hide();
	    });
	    // disable the delete all button
	    $("#check").attr('disabled', 'disabled');
	    if(f == true) {
	      // TO DO -> change it back to del_team_name
	      var dataString = 'status='+req_type;
	      $.ajax({
	      type: "POST",
	      url: "sql.php",
	      data: dataString,
	      cache: false,
	      success: function(html) {
	      }

	    });
	    }//end of if
	  },
	  Cancel: function() {
	    f = false;
	    $(this).dialog("close");
	  }//end of Cancel
	}//end of buttons
      });
  });

// Add new request
  $(".add_btn").click(function() {
    $("#dialog:ui-confirm").dialog("destroy");
    $("#dialog-add").dialog({
	  title: "Δώστε τα στοιχεία:",
	  resizable: false,
	  height:560,
	  width:400,
	  modal: true,
	  buttons: {
	    "OK": function() {
	      var req_name = $("#req_name").val();
	      var pass_num = $("#pass_number").val();
	      var clinic = $("#clinic").val();
	      var addm_num = $("#addm_number").val();
	      var pat_code = $("#patient_code").val();
	      var doctor = $("#doctor").val();
	      var request = $("#req").val();
	      var est_cost = $("#estimated_cost").val();
	      var date_received = $("#date_rec").val();
	      var date_approved = $("#date_app").val();
	      var notes = $("#observations").val();
	      var category = $("#category").val();

	      var dataString = 'req_name=' + req_name + '&pass_num=' + pass_num + '&clinic=' + clinic+ '&addm_num=' + addm_num + 
			      '&pat_code=' + pat_code + '&doctor=' + doctor + '&request=' + request + '&est_cost=' + est_cost +
			      '&date_received=' + date_received + '&date_approved=' + date_approved + '&notes=' + notes + 
			      '&category=' + category;

 	      $.ajax({
 		type: "POST",
 		url: "sql.php",
 		data: dataString,
 		cache: false,
 		success: function(html) {
 		  $("#req_name").html(req_name);
 		  $("#pass_num").html(pass_num);
 		  $("#clinic").html(clinic);
 		  $("#addm_num").html(addm_num);
 		  $("#pat_code").html(pat_code);
 		  $("#doctor").html(doctor);
 		  $("#request").html(request);
 		  $("#est_cost").html(est_cost);
 		  $("#date_received").html(date_received);
 		  $("#date_approved").html(date_approved);
 		  $("#notes").html(notes);
 		  $("#categ").html(category);
 		}
 	      });
	      $(this).dialog("close");
	    },
	    Cancel: function() {
	      $(this).dialog("close");
	    }//end of Cancel
	  }//end of buttons
	});
  });

// Sort table
    var table = $('.requests_table');
    $('#sn, #name, #pass_num, #med_unit, #addm_num, #pat_code, #doc_name, #request, #est_cost, #date_received, #date_approved, #notes')
        .wrapInner('<span title="sort this column"/>')
        .each(function(){ 
            var th = $(this),
                thIndex = th.index(),
                inverse = false;
            th.click(function(){ 
                table.find('td').filter(function(){
                    return $(this).index() === thIndex;
                }).sortElements(function(a, b){ 
                    return $.text([a]) > $.text([b]) ?
                        inverse ? -1 : 1
                        : inverse ? 1 : -1;
                }, function(){
                    // parentNode is the element we want to move
                    return this.parentNode;      
                });              
                inverse = !inverse;     
            });               
        });

// Apply Filter 
//   $(".filter").hover(function() {
//     var ID = $(this).attr('id');
//     $("#"+ID).click(function() {
//       var dataString = 'id=' + ID;
// 
//       $.ajax({
// 	type: "POST",
// 	url: "sql.php",
// 	data: dataString,
// 	cache: false,
// 	success: function(html) {
// 	  $("#req_name").html(req_name);
// 	  $("#pass_num").html(pass_num);
// 	  $("#clinic").html(clinic);
// 	  $("#addm_num").html(addm_num);
// 	  $("#pat_code").html(pat_code);
// 	  $("#doctor").html(doctor);
// 	  $("#request").html(request);
// 	  $("#est_cost").html(est_cost);
// 	  $("#date_received").html(date_received);
// 	  $("#date_approved").html(date_approved);
// 	  $("#notes").html(notes);
// 	  $("#categ").html(category);
// 	}
//       });
//     });
//   });
});//end of ready
</script>
</body>
</html>